
#include <iostream>

#include "Messenger.h"

Messenger::Messenger(std::string remote_ip, int port)
{
    udp_ = new Udp(remote_ip, port);
}

Messenger::~Messenger()
{
    delete udp_;
}

Message& Messenger::Receive(Message& message)
{
    abyte buffer[Message::kMessageSize] = {0};
    udp_->Receive(buffer, sizeof(buffer));
    //printf("Messenger received %s\n", buffer);
    
    int size;
    message = Message::Parse(buffer, &size, message);
    return message;
}

void Messenger::Send(Message& data)
{
    abyte buffer[Message::kMessageSize] = {0};
    //printf("SizeOf Message: %ld\n", sizeof(Message));
    //printf("SizeOf data: %ld\n", sizeof(data));
    data.GetBytes(buffer);
    udp_->Send(buffer, Message::kMessageSize);
}